分类
联系方式
  1. 新浪微博
  2. E-mail

My notes on dart eval

Intro

I recently researched dart_eval and wrote some blogs, and to my surprise, the creator of dart_eval found me and the content of my blog was successfully used as code comments in the project. This makes me very happy and excited, and I plan to use my spare time in the near future to get more involved in this project.

In this note, I have recorded some thoughts and insights that I have just learned about this project.

Learn by Tests

I found the unit test code to be a good introductory learning resource。

How is the code executed dynamically?

from class_test "this" keyword:

There is a special method for testing, inputting source code and outputting a runtime:

final runtime = compiler.compileWriteAndLoad({ // codes });
runtime.executeLib('package:example/main.dart', 'main');

inside compileWriteAndLoad: Step1: Compile the source code into a [Program] containing the parsing result of the AST generated by Dart Analyzer.

final program = compile(packages);

Step2: Generating the dart_eval EVC Bytecode in memory.

final ob = program.write();

Step3: Create a runtime and setting up for parsing the EVC Bytecode.

return Runtime(ob.buffer.asByteData())..setup();

Now that a runtime is ready, the next step is to execute the methods in the runtime via executeLib.

class_test "this" keyword

Dart Code:

'example': {
  'main.dart': '''
    int main () {
      return M(2).load();
    }
    
    class M {
      M(this.number);
      final int number;
      
      int load() {
        return this._loadInternal(4);
      }
      
      _loadInternal(int times) {
        return this.number * times;
      }
    }
  '''
}

units variable in the Compiler.compileSources method:

units: List (1 item)
  [0]: DartCompilationUnit
         declarations: NodeListImpl
            _elements: List (2 items)
              [0]: FounctionDeclearationImpl
                       _functionExpression: FunctionExpressionImpl
                             _body: BlockFunctionBodyImpl
                             _parameters: FormalParameterListImpl
                             _parent: FounctionDeclearationImpl
                             _childEntities: ChildEntities
                                   entities: List (2 items)
                                       [0] ChildEntity
                                             name: "parameters"
                                             value: FormalParameterListImpl
                                       [1] ChildEntity
                                             name: "body"
                                             value BlockFunctionBodyImpl
                            body: same as _body
                            childEntities: same as _childEntities
                            namedChildEntities
                            ……
              [1]: ClassDeclarationImpl
                       _members: NodeListImpl
                           _elements: List (4 items)
                               [0] ConstructorDeclarationImpl
                               [1] FieldDeclarationImpl
                               [2] MethodDeclarationImpl
                               [3] MethodDeclarationImpl
            _owner: CompilationUnitImpl
            exports: List (0 items)
            imports: List (0 items)
            parts: List(0 items)
            uri: _SimpleUri (package:example/main.dart)